Line Generator

Created: 12/8/2022

Tags:

Unity | C# | Personal Work

I created this tool to have lines drawn between different levels quickly without having to edit the line if level portions were ever moved around. The script was designed to create multiple line tracks to deal with branching-level designs. The tool was initially made for 2D games but it can work in a 3D space. The GUI was also made to be simple and easy to understand for people with no coding knowledge.

img
img img img

Line Generator Code:

          
            public class LineGenerator : MonoBehaviour
            {

                public lineCreator[] lineSetUp;

                private void Start()
                {

                    foreach (lineCreator line in lineSetUp)
                    {
                        GameObject newLine = new GameObject(); // Creates GameObject
                        newLine.AddComponent(); // Adds Component
                        newLine.name = line.branchName + " Line"; // Renames
                        newLine.transform.parent = gameObject.transform; // Sets Parent to Scripted Object

                        for (int i = 1; i < line.branch.Length; i++)
                        {
                            CreateLine(newLine, line, line.branch[i-1].position, line.branch[i].position, i);
                        }
                    }
                }



                void CreateLine(GameObject line, lineCreator lineData, Vector3 startPoint, Vector3 endPoint, int index)
                // Moves index vertex  points to each Object in the array
                {

                    // Creates the lineRender Component on GameObject and then inputs all perameters into the new component
                    LineRenderer lr = line.GetComponent();

                    lr.positionCount = lineData.branch.Length;
                    lr.sortingOrder = -1;
                    lr.material = lineData.branchMat;
                    //color
                    lr.startColor = lineData.branchColor;
                    lr.endColor = lineData.branchColor;
                    //width
                    lr.startWidth = lineData.lineWidth;
                    lr.endWidth = lineData.lineWidth;
                    //position
                    lr.SetPosition(index-1, startPoint);
                    lr.SetPosition(index, endPoint);

                    lr.textureMode = LineTextureMode.Tile;

                }

                [System.Serializable]
                public struct lineCreator
                {
                    public string branchName; // User inputed name
                    public Color branchColor; // color of line
                    public Material branchMat; // creates a material for line
                    public float lineWidth; // width of Line
                    public Transform[] branch; // all objects that are on the branch
                }


            }